package com.jzb.binmvvmsdk.http.util;

import com.jzb.binmvvmlib.http.BaseRetrofitEngine;
import com.jzb.binmvvmsdk.config.HttpIp;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * author jzb
 * Date: 2021/4/9
 * Describe:
 */
public class RetrofitEngine extends BaseRetrofitEngine {
    private static RetrofitEngine retrofitEngine;
    private ApiService apiService;

    public RetrofitEngine() {
        initCofig();
    }

    public static RetrofitEngine getRetrofitManager() {
        if (retrofitEngine==null){
            synchronized (RetrofitEngine.class){
                if (retrofitEngine==null){
                    retrofitEngine=new RetrofitEngine();
                }
            }

        }
        return retrofitEngine;
    }

    public ApiService getApiService() {
        if (retrofitEngine==null){
            retrofitEngine=getRetrofitManager();
        }
        return retrofitEngine.apiService;
    }

    @Override
    public void initRetrofit() {
         retrofit= new Retrofit.Builder()
                .baseUrl(HttpIp.webIp)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(okHttpClient)
                .build();
        apiService = retrofit.create(ApiService.class);
    }
}
